<?php

/*
 * This file is part of PHP CS Fixer.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace app\admin\service;

use library\tools\Node;
use think\Db;

/**
 * 系统日志服务管理
 * Class Log.
 */
class Log
{
    /**
     * 写入操作日志.
     *
     * @param string $action
     * @param string $content
     *
     * @return bool
     */
    public static function write($action = '行为', $content = '内容描述')
    {
        $data = [
            'node'     => Node::current(),
            'geoip'    => \PHP_SAPI === 'cli' ? '127.0.0.1' : request()->ip(),
            'action'   => $action,
            'content'  => $content,
            'username' => \PHP_SAPI === 'cli' ? 'cli' : (string) session('user.username'),
        ];

        return false !== Db::name('SystemLog')->insert($data);
    }

    /**
     * 清理系统日志数据.
     *
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     *
     * @return boolean
     */
    public static function clear()
    {
        return false !== Db::name('SystemLog')->where('1=1')->delete();
    }
}
